<template>
  <div>
    <el-dialog title="新增问题" :visible.sync="showAddQuestionDialog" center width="40%">
      <el-form :model="questionData" :rules="rules" ref="questionData">

        <el-form-item label="问题标题" :label-width="formLabelWidth"  prop="title">
          <el-input v-model="questionData.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="问题状态" :label-width="formLabelWidth">
          <el-radio v-model="questionData.status" :label="0">停用</el-radio>
          <el-radio v-model="questionData.status" :label="1">启用</el-radio>
        </el-form-item>

        <el-form-item label="问题答案(文本)" :label-width="formLabelWidth">
          <el-input type="textarea" autosize :rows="2" v-model="questionData.answer" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item :label="'问题答案(图片)'" :label-width="formLabelWidth">
          <div class="upload_head_img">
            <el-upload :show-file-list="false" :before-upload="beforeAvatarUpload" :http-request="upload_picture" class="avatar-uploader" action="">
              <img v-if="questionData.image" :src="questionData.image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"/>
            </el-upload>
          </div>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelSubmit">取 消</el-button>
        <el-button type="primary" @click="submitQuestion('questionData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import store from '@/store'
  import {add} from '@/api/thk/cms/question'
  import Tinymce from '@/components/Tinymce'
  import { client } from '@/utils/alioss'

  export default {
    name: 'addQuestion',
    components: {Tinymce},
    data() {
      return {
        showAddQuestionDialog: false,
        formLabelWidth: '130px',
        questionData: {
          question_id: '',
          title: '',
          answer: '',
          image: '',
          sort_order: '',
          status: 1,
        },

        rules: {
          // 问题为必填字段
          title:[
            { required: true, message: '问题为必填项', trigger: 'blur' },
          ],
        }
      }
    },


    methods: {
      submitQuestion(questionData){
        this.questionData.textOrImage = 1
        this.$refs[questionData].validate((valid) => {
          if (valid) {
            add(store.getters.token, this.questionData).then(result => {
              this.showAddQuestionDialog = !this.showAddQuestionDialog
              this.$message({
                message: '增加成功',
                type: 'success'
              })
              this.$parent.getQuestionList()
            }).catch(error => {
              console.log(error)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },

      cancelSubmit(){
        this.showAddQuestionDialog = !this.showAddQuestionDialog

      },


      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isPNG = file.type === 'image/png'
        const isLt2M = file.size / 1024 / 1024 < 5
        if (!isJPG && !isPNG) {
          this.$message.error('上传图片只能是 JPG、PNG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 5MB!')
        }
        return (isJPG | isPNG) && isLt2M
      },
      upload_picture(file){
        const that = this
        const fileName = 'user-admin_user-picture' + file.file.uid
        client().put(fileName, file.file).then((res) => {
          this.$set(that.questionData, 'image', res.url)
          console.log(res.url);
          that.$message.success('头像地址上传成功！')
        }).catch((err) => {
          that.$message.error('头像地址上传失败，原因：' + err)
        })
      },

    }
  }
</script>

<style>
  /* 图片上传的样式 */
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
  }

  /* 头像地址(head_img)图片上传组件的尺寸 */
  .upload_head_img .avatar-uploader .el-upload{
  }
  .upload_head_img .avatar-uploader-icon{
    font-size: 28px;
    width: 80px;
    height: 80px;
    line-height: 80px;
  }
  .upload_head_img .avatar{
    width: 80px;
    height: 80px;
  }

</style>
